package com.ds.dsm.aggregation.module.service;

import com.ds.common.JDSException;
import com.ds.config.ErrorResultModel;
import com.ds.config.ResultModel;
import com.ds.config.TreeListResultModel;
import com.ds.dsm.aggregation.config.entity.tree.AggEntityConfigTree;
import com.ds.dsm.aggregation.module.ModuleEventNavItem;
import com.ds.dsm.aggregation.module.ModuleWinGroup;
import com.ds.enums.db.MethodChinaName;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.api.enums.CustomCallBack;
import com.ds.esd.custom.bean.CustomModuleBean;
import com.ds.esd.custom.bean.MethodConfig;
import com.ds.esd.custom.enums.CustomMenuItem;
import com.ds.esd.custom.module.annotation.ModuleAnnotation;
import com.ds.esd.custom.nav.annotation.NavTabsViewAnnotation;
import com.ds.esd.dsm.DSMFactory;
import com.ds.esd.dsm.aggregation.AggEntityConfig;
import com.ds.esd.tool.ui.enums.Dock;
import com.ds.esd.util.TreePageUtil;
import net.sf.cglib.beans.BeanMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping(path = "/dsm/agg/entity/config/win/")
public class ModuleConfigService {


    @RequestMapping(method = RequestMethod.POST, value = "WinConfig")
    @NavTabsViewAnnotation(autoSave = true)
    @ModuleAnnotation(imageClass = "spafont spa-icon-rendermode", caption = "窗体配置", dock = Dock.fill, index = 0)
    @APIEventAnnotation(autoRun = true, bindMenu = CustomMenuItem.treeNodeEditor)
    public ResultModel<ModuleWinGroup> getModuleWinGroup(String sourceClassName, String methodName, String domainId) {
        ResultModel<ModuleWinGroup> result = new ResultModel<ModuleWinGroup>();
        return result;
    }


    @MethodChinaName(cname = "编辑数据信息")
    @RequestMapping(method = RequestMethod.POST, value = "updateWinData")
    @APIEventAnnotation(bindMenu = CustomMenuItem.save, callback = {CustomCallBack.Reload})
    public @ResponseBody
    ResultModel<Boolean> updateWinData(@RequestBody CustomModuleBean bean) {
        ResultModel<Boolean> result = new ResultModel<Boolean>();
        try {
            AggEntityConfig classConfig = DSMFactory.getInstance().getAggregationManager().getAggEntityConfig(bean.getSourceClassName(), bean.getDomainId());
            MethodConfig methodAPIBean = classConfig.getMethodByName(bean.getMethodName());
            CustomModuleBean customModuleBean = methodAPIBean.getModuleBean();
            BeanMap.create(customModuleBean).putAll(BeanMap.create(bean));
            DSMFactory.getInstance().getAggregationManager().updateAggEntityConfig(classConfig);
        } catch (JDSException e) {
            ErrorResultModel<Boolean> errorResult = new ErrorResultModel<Boolean>();
            errorResult.setErrdes(e.getMessage());
            result = errorResult;
        }
        return result;
    }

    @MethodChinaName(cname = "清空窗体配置")
    @RequestMapping(method = RequestMethod.POST, value = "clear")
    @APIEventAnnotation(bindMenu = CustomMenuItem.formReSet, callback = {CustomCallBack.Reload})
    public @ResponseBody
    ResultModel<Boolean> clear(String sourceClassName, String methodName, String domainId) {
        ResultModel<Boolean> result = new ResultModel<Boolean>();
        try {
            AggEntityConfig classConfig = DSMFactory.getInstance().getAggregationManager().getAggEntityConfig(sourceClassName, domainId);
            MethodConfig methodAPIBean = classConfig.getMethodByName(methodName);
            methodAPIBean.setModuleBean(null);
            DSMFactory.getInstance().getAggregationManager().updateAggEntityConfig(classConfig);
        } catch (JDSException e) {
            ErrorResultModel<Boolean> errorResult = new ErrorResultModel<Boolean>();
            errorResult.setErrdes(e.getMessage());
            result = errorResult;
        }

        return result;
    }


    @RequestMapping(method = RequestMethod.POST, value = "loadModuleNav")
    @APIEventAnnotation(bindMenu = CustomMenuItem.loadChild)
    @ResponseBody
    public TreeListResultModel<List<AggEntityConfigTree>> loadWinNav(String sourceClassName, String domainId) {
        TreeListResultModel<List<AggEntityConfigTree>> result = new TreeListResultModel<>();
        result = TreePageUtil.getTreeList(Arrays.asList(ModuleEventNavItem.values()), AggEntityConfigTree.class);
        return result;
    }


}
